package com.wushi.lenovo.asia5b.app_ui.shopcar.fragment;

import android.content.Intent;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mvp.common.LocalManageUtil;
import com.mvp.helper.WCallBack;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_model.good.goodbean.GoodsModel;
import com.wushi.lenovo.asia5b.app_model.good.ShopModel;
import com.wushi.lenovo.asia5b.app_ui.good.activity.OrderCheckPiActivity;
import com.wushi.lenovo.asia5b.app_ui.other.dialog.ShopCarNoticeDialog;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;

/**
 * Created by zhangyuncai on 2020/3/3.
 */
public class ShopCarPiFragment extends ShopCarFragment {
    @BindView(R.id.iv_shopcarpi_normalitem)
    ImageView iv_shopcarpi_normalitem;
    @BindView(R.id.tv_shopcarpi_n)
    TextView tv_shopcarpi_n;
    @BindView(R.id.ll_shopcarpi_normalitem)
    LinearLayout ll_shopcarpi_normalitem;
    @BindView(R.id.iv_shopcarpi_sensitiveitem)
    ImageView iv_shopcarpi_sensitiveitem;
    @BindView(R.id.tv_shopcarpi_s)
    TextView tv_shopcarpi_s;
    @BindView(R.id.ll_shopcarpi_sensitiveitem)
    LinearLayout ll_shopcarpi_sensitiveitem;
    @BindView(R.id.ll_shopcarpi_top)
    LinearLayout ll_shopcarpi_top;

    @Override
    protected boolean isDaigou() {
        return false;
    }

    @Override
    protected void initView() {
        super.initView();
        setSpecial();
    }

    /**
     * 对普通商品/敏感商品textview设置特殊效果
     */
    private void setSpecial() {
        tv_shopcarpi_n.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        tv_shopcarpi_n.getPaint().setAntiAlias(true);//抗锯齿z
        tv_shopcarpi_s.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        tv_shopcarpi_s.getPaint().setAntiAlias(true);//抗锯齿z

        if (TextUtils.equals("en", LocalManageUtil.getAppLanguage())) {//如果是英文环境
            SpannableString spanableInfo = new SpannableString(getString(R.string.order_normalitem));
            RelativeSizeSpan bigSizeSpan = new RelativeSizeSpan(1.1f);//将原来的文字放大倍数
            spanableInfo.setSpan(bigSizeSpan, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//将原来的文字放大倍数
            spanableInfo.setSpan(new StyleSpan(Typeface.BOLD), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//加粗
            tv_shopcarpi_n.setText(spanableInfo);

            spanableInfo = new SpannableString(getString(R.string.order_sensitiveitem));
            spanableInfo.setSpan(bigSizeSpan, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//将原来的文字放大倍数
            spanableInfo.setSpan(new StyleSpan(Typeface.BOLD), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//加粗
            tv_shopcarpi_s.setText(spanableInfo);
        }

    }


    /**
     * 设置复选框状态
     */
    private void setCheckView() {

        int normalNum = 0;//普通商品数量
        int sensetiveNum = 0;//敏感商品数量
        int normalCheckedNum = 0;//普通商品选中的数量
        int sensetiveCheckedNum = 0;//敏感商品选中的数量
        for (ShopModel shopModel : mDatas) {
            if (shopModel.products != null) {
                for (GoodsModel model : shopModel.products) {
                    if(model.canCheck()) {
                        if (model.isMingan()) {
                            sensetiveNum++;
                            if (model.isChecked()) {
                                sensetiveCheckedNum++;
                            }
                        } else {
                            normalNum++;
                            if (model.isChecked()) {
                                normalCheckedNum++;
                            }
                        }
                    }
                }
            }
        }

        //普通商品全选框
        iv_shopcarpi_normalitem.setSelected((normalCheckedNum == normalNum && normalCheckedNum > 0));
        //敏感商品全选框
        iv_shopcarpi_sensitiveitem.setSelected((sensetiveCheckedNum == sensetiveNum && sensetiveCheckedNum > 0));
        //购物车商品数==0
        if (mDatas.size() == 0) {
            iv_shopcarpi_normalitem.setSelected(false);
            iv_shopcarpi_sensitiveitem.setSelected(false);
            ll_shopcarpi_top.setVisibility(View.GONE);
        } else {
            ll_shopcarpi_top.setVisibility(View.VISIBLE);
        }
    }


    @Override
    protected void setBottomView() {
        super.setBottomView();
        //选中数据
        List<GoodsModel> selectData = getSelectData();
        if (selectData.size() != 0) {

        } else {
            iv_shopcarpi_normalitem.setSelected(false);
            iv_shopcarpi_sensitiveitem.setSelected(false);
        }
    }

    @Override
    public void getCharges() {
        setCheckView();
        super.getCharges();
    }

    @Override
    protected void getGoodList(int refresh) {
        getPresenter().getGoodListPi(refresh, mDatas);
    }

    @Override
    protected void submit() {
        List<GoodsModel> selectData = getSelectData();

        if (selectData.size() != 0) {
            ShopCarNoticeDialog noticeDialog = new ShopCarNoticeDialog(getContext(),
                    String.format(getString(R.string.shopcar_submitnotice_title), selectData.size()),
                    "");
            noticeDialog.setCallBack(new WCallBack<Boolean>() {
                @Override
                public void callback(Boolean aBoolean) {
                    if (aBoolean) {
                        Intent intent = new Intent(getContext(), OrderCheckPiActivity.class);
                        intent.putExtra("datas", (Serializable) getSelectIdList());
                        intent.putExtra("isDaigouGoods", false);
                        getContext().startActivity(intent);
                    }
                }
            });
            noticeDialog.show();

        }
    }

    /**
     * 普通/敏感商品全选
     *
     * @param isNormal 是否是普通商品
     */
    protected void checkAll(boolean isNormal) {
        for (ShopModel shopModel : mDatas) {
            shopModel.setChecked(false);//所有商品设置未选中状态
        }
        for (ShopModel shopModel : mDatas) {
            if (shopModel.products != null) {
                for (GoodsModel goodsModel : shopModel.products) {
                    if (isNormal && (!goodsModel.isMingan())) {
                        goodsModel.setChecked(true);
                    } else if ((!isNormal) && (goodsModel.isMingan())) {
                        goodsModel.setChecked(true);
                    } else {
                        goodsModel.setChecked(false);
                    }
                }
            }
        }
        getCharges();
        mAdapter.notifyDataSetChanged();
    }

    @Override
    protected void initEvent() {
        super.initEvent();

        View.OnClickListener checkAllNormalListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAll(true);
            }
        };
        ll_shopcarpi_normalitem.setOnClickListener(checkAllNormalListener);
        iv_shopcarpi_normalitem.setOnClickListener(checkAllNormalListener);
        View.OnClickListener checkAllSensitiveListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAll(false);
            }
        };
        ll_shopcarpi_sensitiveitem.setOnClickListener(checkAllSensitiveListener);
        iv_shopcarpi_sensitiveitem.setOnClickListener(checkAllSensitiveListener);

    }

}
